<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>accredit 权限</title>
	</head>
	<link rel="stylesheet" href="../css/common.css" />
	<link rel="stylesheet" href="../css/demo.css" type="text/css">
	<link rel="stylesheet" href="../css/zTreeStyle.css" type="text/css">
	<style>
		body{
			background:#eeeeee;
		}
		.Main{
			height:350px;
			width:400px;
			background:white;
			border:1px solid #ddd;
			margin:0 0 0 15px;			
		}
		.title{
			line-height:35px;
			margin-left:15px;
			font-family:"微软雅黑";
		}
		.title b{
			float:right;
		}
		.title span{
			margin-right:14px;
			cursor:pointer;
			color:#00aa98;
		}
		.title span:hover{
			color:red;
		}
		
		#treeDemo{
			background:white;
			margin-top:0;
			border:none;
			height:350px;
			width:390px;
		}
	</style>
	<body>
		<p class='title'>系统资源<!--<b><span class='checkall'>全选</span><span class='inverseCheck'>反选</span><span class='cancle'>取消</span></b>  --></p>
		<div st class='Main '>
			<ul id="treeDemo" class="ztree"></ul>
		</div>
		<div style='padding:20px 0;text-align:center;'><i class='Dolo_btn accredit'>授权</i></div>
		<script type="text/javascript" src="../js/jquery-1.12.4.js" ></script>
		<script type="text/javascript" src="../layer-v2.4/layer/layer.js" ></script>
		<script type="text/javascript" src="../js/common.js" ></script>
		<script type="text/javascript" src="../js/jquery.ztree.core.js"></script>
		<script type="text/javascript" src="../js/jquery.ztree.excheck.js"></script>
		<script type="text/javascript" src="../js/My97DatePicker/WdatePicker.js" charset="utf-8"></script>
		<script>
//_________________________________________________________________渲染页面	
		var postId = getHref().postId-0
		var zNodes
		$.ajax({
			type:"get",
			url:"../role/getauth?id="+postId+"&temp="+Math.random(),			
			async:false,
			success:function(data){
				data = JSON.parse(data)
				zNodes = data.data.resourceList
				
				
			}			
		});	
		
//_________________________________________________________________全选
				$(".checkall").click(function(){
							
				})							
//_________________________________________________________________反选
				$(".inverseCheck").click(function(){
							
				})
//_________________________________________________________________取消
				$(".cancle").click(function(){
					
				})
//_________________________________________________________________授权
				$(".accredit").click(function(){					
					var submitMess = {};
					submitMess.id = postId;
					submitMess.resourceList = [];
					var idArr = onCheck();
					for(var i=0;i<idArr.length;i++){
						var json = {};
						json.id = idArr[i];
						submitMess.resourceList.push(json)
					}
					layer.msg('授权中...', {icon: 16,shade: 0.01});
					$.ajax({
						type:"post",
						url:"../role/grant?temp="+Math.random(),
						contentType:'application/json;charset=utf-8',
						async:true,
						data:JSON.stringify(submitMess),
						success:function(data){
							data=JSON.parse(data)
							if(data.status=="1001"){
								layer.msg('授权成功', {icon: 1,time:1000},function(){
									var index = parent.layer.getFrameIndex(window.name);//获取弹窗索引
									parent.layer.close(index);//关闭弹窗
								})	
							}if(data.status=="1002"){
								layer.msg("授权失败,"+data.message,{icon: 2,time:2000},function(){
									
								})
							}
						}
							
					});	
				})
				
				
				
				
			
//_________________________________________________________________zTree		
		var setting = {
			check: {
				enable: true,
				chkboxType :{ "Y" : "p", "N" : "s" }
			},
			data: {
				key: {
					title: "title"
				},
				simpleData: {
					enable: true
				}
			},
			callback: {
				onCheck: onCheck
			}
			
		};
		
		
		function zTreeOnClick(event, treeId, treeNode, clickFlag){
			
		}	
		function onCheck(e, treeId, treeNode) {
			count();
		} 

		 function setTitle(node) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			var nodes = node ? [node]:zTree.transformToArray(zTree.getNodes());
			for (var i=0, l=nodes.length; i<l; i++) {
				var n = nodes[i];
				n.title = "[" + n.id + "] isFirstNode = " + n.isFirstNode + ", isLastNode = " + n.isLastNode;
				zTree.updateNode(n);
			}
		} 
		function count() {
			function isForceHidden(node) {
				if (!node.parentTId) return false;
				var p = node.getParentNode();
				return !!p.isHidden ? true : isForceHidden(p);
			}
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			checkCount = zTree.getCheckedNodes(true).length,
			nocheckCount = zTree.getCheckedNodes(false).length,
			hiddenNodes = zTree.getNodesByParam("isHidden", true),
			hiddenCount = hiddenNodes.length;

			for (var i=0, j=hiddenNodes.length; i<j; i++) {
				var n = hiddenNodes[i];
				if (isForceHidden(n)) {
					hiddenCount -= 1;
				} else if (n.isParent) {
					hiddenCount += zTree.transformToArray(n.children).length;
				}
			}

			 $("#isHiddenCount").text(hiddenNodes.length);
			$("#hiddenCount").text(hiddenCount);
			$("#checkCount").text(checkCount);
			$("#nocheckCount").text(nocheckCount); 
		}
		function showNodes() {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getNodesByParam("isHidden", true);
			zTree.showNodes(nodes);
			setTitle();
			count();
		}
		function hideNodes() {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getSelectedNodes();
			if (nodes.length == 0) {
				alert("Please select one node at least.");
				return;
			}
			zTree.hideNodes(nodes);
			setTitle();
			count();
		}

		$(document).ready(function(){
			$.fn.zTree.init($("#treeDemo"), setting, zNodes);
			$("#hideNodesBtn").bind("click", {type:"rename"}, hideNodes);
			$("#showNodesBtn").bind("click", {type:"icon"}, showNodes);
			setTitle();
			count();
		});
		
		
		//_______________________________________________________________获取选中节点的id
		function onCheck(e,treeId,treeNode){
            var treeObj=$.fn.zTree.getZTreeObj("treeDemo"),
            idArr = []
            nodes=treeObj.getCheckedNodes(true),
            v="";
            for(var i=0;i<nodes.length;i++){
                v+=nodes[i].name + ",";
                idArr.push(nodes[i].id); //获取选中节点的值
            }
            return idArr;
        }   
		
	
		</script>
	</body>
</html>
